<?php
namespace App\Admin\Controllers\FileModel;
use App\Http\Controllers\Controller;
use Dcat\Admin\FormStep\Form as StepForm;
use App\Models\InputStepForm;
use App\Models\InformationList;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use Dcat\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Log;
// use Dcat\Admin\Widgets\Form;
class ProjectDailyReportController extends Controller
{
    //工作日程表 云之家
    protected $options = [
        1 => '显示文本框',
        2 => '显示编辑器',
        3 => '显示文件上传',
        4 => '还是显示文本框',
    ];
    // use PreviewCode;
    public function index(Content $content)
    {
        return $content
            // ->body('<div style="margin:5px 0 15px;">'.$this->buildPreviewButton().'</div>')
            ->body($this->form())
            ->header('5.2-工程日报表（云之家）')
            ->description('分步表单');
    }
    protected function form()
{
    Admin::js('/stepform/js/jquery.smartWizard.min.js');
    Admin::css('/stepform/css/step.css');
    return Form::make(new InputStepForm(), function (Form $form) {
            $form->title('电除尘改造信息调查表数据录入');
            $form->action('ProjectDailyReportstep');
            $form->disableListButton();
            $form->multipleSteps()
                ->remember() 
                ->width('100%')
                ->add('人员设备', function (StepForm $step) use ($form) {
                    $step->text('wsry',trans('我司人员数'))->required()->type('number');
                    $step->text('glry',trans('施工队伍管理人员数量'))->required()->type('number');
                    $step->text('hg',trans('焊工人数'))->required()->type('number');
                    $step->text('dg',trans('电工人数'))->required()->type('number');
                    $step->text('lg',trans('力工人数'))->required()->type('number');
                    $step->text('jlhj',trans('交流焊机数量'))->required()->type('number');
                    $step->text('qcd',trans('25吨汽车吊数量'))->required()->type('number');
                    $step->text('qtsy',trans('其他事宜'))->required();
                    // $step->multipleImage('jdzp', trans('进度照片'))->autoUpload()->url('step/files');
                })
                ->add('工程计划', function ($step) {   
                    $step->table('pa1', trans('工程报表'),function ($table) {
                        $table->text('分项工程名称')->width(11, 1)->required();
                        $table->text('计划起止日期')->required();
                        $table->text('实际起止日期')->required();
                        $table->text('当日计划')->required();
                        $table->text('当日实际进度')->required();
                        $table->text('次日计划')->required();
                        $table->text('进度偏差分析')->required();
                        $table->text('措施')->required();
                    })->width(12,0)->rules('required');

                    $step->table('pa2', trans('到货情况'),function ($table) {
                        $table->text('名称')->width(8, 2)->required();
                        $table->text('供应商')->required();
                        $table->text('供货方式')->required();
                        $table->text('开箱情况')->required();
                        $table->text('外观')->required();
                        $table->text('包装情况')->required();
                    })->width(12,0)->rules('required');

                    $step->table('pa3', trans('质量检查'),function ($table) {
                        $table->text('构件名称')->width(8, 2)->required();
                        $table->text('检查项目a')->required();
                        $table->text('检查情况a')->required();
                    })->width(12,0)->rules('required');    

                    $step->table('pa4', trans('资料情况'),function ($table) {
                        $table->text('资料名称')->width(8, 2)->required();
                        $table->text('报验情况')->required();
                        $table->text('是否可归档')->required();
                    })->width(12,0)->rules('required');

                    $step->table('pa5', trans('安全检查'),function ($table) {
                        $table->text('检查项目b')->width(8, 2)->required();
                        $table->text('检查情况b')->required();
                    })->width(12,0)->rules('required');
                })
                ->add('问题反馈', function ($step) {
                    $step->table('pa6', trans('问题反馈'),function ($table) {
                        $table->text('fkrq','反馈日期')->width(10, 2)->required();
                        $table->text('wt','需要解决的问题')->required();
                        $table->text('clsj','要求处理完毕时间')->required();
                        $table->text('clbm','处理部门')->required();
                        $table->text('jbr','经办人')->required();
                        $table->text('clyj','处理意见')->required();
                        $table->text('clwbsj','处理完毕时间')->required();
                        $table->text('cljg','处理结果')->required();
                        $table->text('wwcyy','未完成原因')->required();
                    })->width(12,0)->rules('required');
                });
        });
    }

    public function step(Request $request){
        $info = $request->all();
        if(empty($info['CURRENT_VALIDATION_STEP'])&&$info['ALL_STEPS']==1){//提交
            Log::info($info);
            //实例化 phpword 类
            $PHPWord = new PhpWord();
            //指定事先制作好的模板文件路径
            $templateProcessor = new TemplateProcessor(public_path("filemodel/project_daily_report.docx"));
            if(isset($info['pa1'])){
                $rowsa=count($info['pa1']);//总行数
                $templateProcessor->cloneRow('number',$rowsa);//复制行
                $pa1 = $info['pa1'];
                $pa1 = array_values($pa1);
                for($a=0;$a<$rowsa;$a++){
                    $templateProcessor->setValue("number#".($a+1),$a+1);//替换变量
                    $templateProcessor->setValue("fxgcmc#".($a+1),$pa1[$a]['分项工程名称']);//替换变量
                    $templateProcessor->setValue("jhqzrq#".($a+1),$pa1[$a]['计划起止日期']);
                    $templateProcessor->setValue("sjqzrq#".($a+1),$pa1[$a]['实际起止日期']);
                    $templateProcessor->setValue("drjh#".($a+1),$pa1[$a]['当日计划']);
                    $templateProcessor->setValue('drsjjd#'.($a+1), $pa1[$a]['当日实际进度']);
                    $templateProcessor->setValue('crjh#'.($a+1), $pa1[$a]['次日计划']);
                    $templateProcessor->setValue('jdpcfx#'.($a+1), $pa1[$a]['进度偏差分析']);
                    $templateProcessor->setValue('cs#'.($a+1), $pa1[$a]['措施']);
                }
            }
            if(isset($info['pa2'])){
                $rowsb=count($info['pa2']);//总行数
                $templateProcessor->cloneRow('mc',$rowsb);//复制行
                $pa2 = $info['pa2'];
                $pa2 = array_values($pa2);
                for($i=0;$i<$rowsb;$i++){
                    $templateProcessor->setValue("mc#".($i+1),$pa2[$i]['名称']);//替换变量
                    $templateProcessor->setValue("gys#".($i+1),$pa2[$i]['供应商']);
                    $templateProcessor->setValue("ghfs#".($i+1),$pa2[$i]['供货方式']);
                    $templateProcessor->setValue("kxqk#".($i+1),$pa2[$i]['开箱情况']);
                    $templateProcessor->setValue('wg#'.($i+1), $pa2[$i]['外观']);
                    $templateProcessor->setValue('bzqk#'.($i+1), $pa2[$i]['包装情况']);
                }
            }
            if(isset($info['pa3'])){
                $rows3=count($info['pa3']);//总行数
                $templateProcessor->cloneRow('构件名称',$rows3);//复制行
                $pa3 = $info['pa3'];
                $pa3 = array_values($pa3);
                for($c=0;$c<$rows3;$c++){
                    $templateProcessor->setValue("构件名称#".($c+1),$pa3[$c]['构件名称']);//替换变量
                    $templateProcessor->setValue("检查项目a#".($c+1),$pa3[$c]['检查项目a']);
                    $templateProcessor->setValue("检查情况a#".($c+1),$pa3[$c]['检查情况a']);
                }
            }
            if(isset($info['pa4'])){
                $row4=count($info['pa4']);//总行数
                $templateProcessor->cloneRow('zlmc',$row4);//复制行
                $pa4 = $info['pa4'];
                $pa4 = array_values($pa4);
                for($d=0;$d<$row4;$d++){
                    $templateProcessor->setValue("zlmc#".($d+1),$pa4[$d]['资料名称']);//替换变量
                    $templateProcessor->setValue("byqk#".($d+1),$pa4[$d]['报验情况']);
                    $templateProcessor->setValue("sfkgd#".($d+1),$pa4[$d]['是否可归档']);
                }
            }
            if(isset($info['pa5'])){
                $rows5=count($info['pa5']);//总行数
                $templateProcessor->cloneRow('检查项目b',$rows5);//复制行
                $pa5 = $info['pa5'];
                $pa5 = array_values($pa5);
                for($e=0;$e<$rows5;$e++){
                    $templateProcessor->setValue("检查项目b#".($e+1),$pa5[$e]['检查项目b']);//替换变量
                    $templateProcessor->setValue("检查情况b#".($e+1),$pa5[$e]['检查情况b']);
                }
            }
            if(isset($info['pa6'])){
                $rows6=count($info['pa6']);//总行数
                $templateProcessor->cloneRow('nums',$rows6);//复制行
                $pa6 = $info['pa6'];
                $pa6 = array_values($pa6);
                for($f=0;$f<$rows6;$f++){
                    $templateProcessor->setValue("nums#".($f+1),$f+1);//替换变量
                    $templateProcessor->setValue("fkrq#".($f+1),$pa6[$f]['fkrq']);
                    $templateProcessor->setValue("wt#".($f+1),$pa6[$f]['wt']);
                    $templateProcessor->setValue("clsj#".($f+1),$pa6[$f]['clsj']);
                    $templateProcessor->setValue("clbm#".($f+1),$pa6[$f]['clbm']);
                    $templateProcessor->setValue("jbr#".($f+1),$pa6[$f]['jbr']);
                    $templateProcessor->setValue("clyj#".($f+1),$pa6[$f]['clyj']);
                    $templateProcessor->setValue("clwbsj#".($f+1),$pa6[$f]['clwbsj']);
                    $templateProcessor->setValue("cljg#".($f+1),$pa6[$f]['cljg']);
                    $templateProcessor->setValue("wwcyy#".($f+1),$pa6[$f]['wwcyy']);
                }
            }
           
            $templateProcessor->setValue('wsry',$info['wsry']);
            $templateProcessor->setValue('glry',$info['glry']);
            $templateProcessor->setValue('dg',$info['dg']);
            $templateProcessor->setValue('hg',$info['hg']);
            $templateProcessor->setValue('lg',$info['lg']);
            $templateProcessor->setValue('jlhj',$info['jlhj']);
            $templateProcessor->setValue('qcd',$info['qcd']);
            $templateProcessor->setValue('qtsy',$info['qtsy']);

            
            unset($info['_file_']);
            unset($info['CURRENT_VALIDATION_STEP']);
            unset($info['ALL_STEPS']);
            unset($info['_token']);
            $data['admin_id'] = Admin::user()->id;
            $data['name'] = '工程日报表'.date('Y-m-d H:i');
            $data['data'] = $info;
            // $data['content'] = json_encode($info);
            // $insertData['content'] = json_encode($data);
            $insertData['name'] = $data['name'];
            $insertData['type'] = 3;
            $insertData['admin_id'] = Admin::user()->id;
            $insertData['created_at'] = date('Y-m-d H:i:s');
            // $templateProcessor->saveAs(public_path('filemodellist/'.$info['projectname'].md5(time()) . ".docx"));
            $templateProcessor->saveAs(public_path('filemodellist/'.$data['name'].".docx"));
            InformationList::insert($insertData);
            return $this->form()->saving(function (Form $form) {
                // 清空缓存
                $form->multipleSteps()->flushStash();
                // 拦截保存操作
                return response(
                    $form->multipleSteps()
                        ->done()
                        ->render()
                );
            })->store();
        }
    }

    /**
     * 保存
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $requset)
    {
        return $this->form()->saving(function (Form $form) {
            
            // 清空缓存
            $form->multipleSteps()->flushStash();
            // 拦截保存操作
            return response(
                $form->multipleSteps()
                    ->done()
                    ->render()
            );
        })->store();
    }

    public function list($id,Content $content){
        return $content
            ->header('表格列表')
            ->description()
            ->body($this->grid($id));
    }
    protected function grid($typeid)
    {
        return new Grid(InformationList::where(['admin_id'=>Admin::user()->id,'type'=>$typeid]), function (Grid $grid) {
            $grid->showActions();
            $grid->disableDeleteButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->fixColumns(2);
            
            $grid->column('id', 'ID');
            $grid->column('name', '名称');
            $grid->column('type','类型')->display(function($type) {
                return Db::table('xfy_info_model')->where(['id'=>$type])->value('name');;
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();
                $edit_url = admin_base_path('downloadfile/'.$id);
                $actions->append("<a href='$edit_url'>下载</a>");
            });
        });
    }
}